package com.thinkgem.javamg.modules.busi.crawler;

import com.thinkgem.javamg.common.utils.SpringContextHolder;
import com.thinkgem.javamg.common.utils.StringUtils;
import com.thinkgem.javamg.modules.busi.dao.NewsDao;
import com.thinkgem.javamg.modules.busi.entity.News;
import com.thinkgem.javamg.modules.busi.entity.NewsAlbum;
import com.thinkgem.javamg.modules.busi.service.NewsService;
import com.thinkgem.javamg.modules.busi.utils.DateConverUtil;
import com.thinkgem.javamg.modules.busi.utils.ImageUtil;
import com.thinkgem.javamg.modules.busi.utils.JsoupUtil;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * 新浪相关爬取
 */
public class CrawSina {

    protected static Logger logger = LoggerFactory.getLogger(CrawSina.class);
    private static NewsDao newsDao = SpringContextHolder.getBean(NewsDao.class);
    private static NewsService newsService = SpringContextHolder.getBean(NewsService.class);

    /**
     * 首页图文资讯列表
     *
     * @return
     */
    public void newsIndexList() {
        try {
            Document doc = JsoupUtil.getDocument("http://golf.sina.cn/");
            Element element = doc.getElementById("j_nav72835_list");
            Elements aElements = element.getElementsByTag("a");
            for (Element aElement : aElements) {
                //组装详情
                News news = null;
                try {
                    String title = JsoupUtil.getFirstElementText(aElement.select("h4[class=f_card_h4]"));
                    String detailUrl = aElement.attr("href");//资讯详情地址
                    if (newsDao.isexist(title) > 0 || detailUrl.contains("video.")) {
                        continue;
                    }

                    if (aElement.select("ul[class=f_three_pic]").size() > 0) {//处理图集
                        news = gqphotoDetail(detailUrl);
                    } else {
                        //下载列表图片
                        String smallPicUrl = JsoupUtil.getFirstElementAttrText(aElement.getElementsByClass("f_card_dt_img"), "data-src");
                        if (StringUtils.isNotEmpty(smallPicUrl) && !smallPicUrl.contains("http:")) {
                            smallPicUrl = "http:" + smallPicUrl;
                        }
                        String imgPath = ImageUtil.downloadImageToDisk(smallPicUrl);

                        //获取资讯详情内容
                        news = newsDetail(detailUrl);
                        news.setSmallpicurl(imgPath);
                    }
                    news.setTitle(title);
                } catch (Exception e) {
                    logger.error(e.getMessage());
                }

                //插入数据库
                try {
                    newsService.save(news);
                } catch (Exception e) {
                    logger.error(e.getMessage());
                }

                //等待
                Thread.sleep(Constants.millis);
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
    }

    /**
     * 图文详情
     *
     * @return
     */
    public News newsDetail(String url) {
        News news = new News();
        try {
            Document doc = JsoupUtil.getDocument(url);
            Element articleElement = doc.select("article[class=art_box]").first();

            //下载详情第一张图为头图
            String imgSrc = JsoupUtil.getFirstElementAttrText(articleElement.select("img[class=j_fullppt]"), "data-src");
            if (StringUtils.isNotEmpty(imgSrc) && !imgSrc.contains("http:")) {
                imgSrc = "http:" + imgSrc;
            }
            String imgPath = ImageUtil.downloadImageToDisk(imgSrc);
            news.setPicurl(imgPath);

            //处理段落内容
            Elements aElements = articleElement.select("a[href=JavaScript:void(0)]");
            if (null != aElements && aElements.size() > 0) {
                aElements.get(aElements.size() - 1).remove();
            }
            JsoupUtil.removeElements(articleElement.getElementsByTag("section"));
            JsoupUtil.removeElements(articleElement.getElementsByTag("aside"));
            JsoupUtil.removeElement(articleElement.select("img[class=sharePic hide]").first());
            JsoupUtil.removeElement(articleElement.select("h1[class=art_tit_h1]").first());
            JsoupUtil.removeElement(articleElement.select("time[class=art_time]").first());
            JsoupUtil.removeElement(articleElement.select("div[id=wx_pic]").first());
            JsoupUtil.removeElement(articleElement.select("script[type=text/javascript]").first());
            articleElement.removeClass("art_p");

            Elements figureElements = articleElement.select("figure[data-type=simple]");
            for (Element figureElement : figureElements) {
                Element imgElement = figureElement.select("img[class~=^art_img_mini_img]").first();
                if (null != imgElement) {
                    String path = imgElement.attr("data-src");
                    if (StringUtils.isNotEmpty(path) && !path.contains("http:")) {
                        path = "http:" + path;
                    }
                    imgElement.removeAttr("class");
                    imgElement.removeAttr("src");
                    imgElement.removeAttr("data-src");
                    imgElement.attr("src", path);
                    figureElement.replaceWith(imgElement);
                }

            }
            news.setContent(articleElement.html().replace("class=\"art_p\"", ""));
            news.setAddtime(DateConverUtil.conver(JsoupUtil.getFirstElementOwnText(articleElement.select("time[class=art_time]"))));
            news.setSource(JsoupUtil.getFirstElementText(articleElement.select("cite[class=art_cite]")));
            news.setTypeId(2);
            if (StringUtils.isEmpty(news.getSource())) {
                news.setSource("新浪体育");
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return news;
    }

    /**
     * 高清图列表
     *
     * @return
     */
    public void gqphotoList() {
        try {
            Document doc = JsoupUtil.getDocument("https://photo.sina.cn/channel?vt=4&pos=67&ch=2&cid=golf");
            Elements asideElements = doc.select("aside[class=hide_info]");
            for (Element asideElement : asideElements) {
                //组装详情
                News news = null;
                try {
                    String title = JsoupUtil.getFirstElementAttrText(asideElement.getElementsByTag("a"), "title");
                    if (newsDao.isexist(title) > 0) {
                        continue;
                    }
                    news = gqphotoDetail(JsoupUtil.getFirstElementAttrText(asideElement.getElementsByTag("a"), "href"));
                    news.setTitle(title);
                } catch (Exception e) {
                    logger.error(e.getMessage());
                }

                //插入数据库
                try {
                    newsService.save(news);
                } catch (Exception e) {
                    logger.error(e.getMessage());
                }

                //等待
                Thread.sleep(Constants.millis);
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
    }

    /**
     * 高清图详情
     *
     * @return
     */
    public News gqphotoDetail(String url) {
        News news = new News();
        List<NewsAlbum> newsAlbumList = new ArrayList<>();
        try {
            Document doc = JsoupUtil.getDocument(url);
            Element articleElement = doc.select("article[class=s_card]").get(0);
            Elements sectionElements = articleElement.select("section[class=section-item]");

            news.setAddtime(DateConverUtil.conver(articleElement.select("time[class=hd_h2_d]").text()));
            news.setTypeId(6);
            int imgSize = sectionElements.size();
            int i = 0;
            for (Element sectionElement : sectionElements) {
                NewsAlbum newsAlbum = new NewsAlbum();
                String picUrl = JsoupUtil.getFirstElementAttrText(sectionElement.select("img[class=img_width j_ph]"), "src");
                if (StringUtils.isNotEmpty(picUrl) && picUrl.contains("data:image")) {
                    picUrl = JsoupUtil.getFirstElementAttrText(sectionElement.select("img[class=img_width j_ph]"), "data-src");
                }
                String imgPath = ImageUtil.downloadImageToDisk(picUrl);
                newsAlbum.setPicUrl(imgPath);
                newsAlbum.setPicUrlThumb(imgPath);
                newsAlbum.setSort(imgSize--);

                //取第一张图集的内容、图片做资讯内容、图片
                if (i == 0) {
                    String content = sectionElement.select("p[class=hd_img_info]").text();
                    content = content.substring(sectionElement.select("em[class=hd_img_info_n]").text().length(), content.length());
                    news.setContent(content);
                    news.setPicurl(imgPath);
                    news.setSmallpicurl(imgPath);
                    news.setSource("新浪体育");
                }
                newsAlbumList.add(newsAlbum);
                i++;
            }
            news.setNewsAlbumList(newsAlbumList);
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return news;
    }


    /**
     * 球技列表
     *
     * @return
     */
    public List<News> skillList() {
        List<News> list = new ArrayList<>();
        try {
            Document doc = JsoupUtil.getDocument("http://sports.sina.cn/golf/57335/index.d.html?vt=4&pos=67");
            Elements aElements = doc.getElementById("j_nav72836_list").getElementsByTag("a");
            for (Element aElement : aElements) {
                //组装详情
                News news = null;
                try {
                    String title = JsoupUtil.getFirstElementText(aElement.select("h4[class=f_card_h4]"));
                    String detailUrl = aElement.attr("href");
                    if (newsDao.isexist(title) > 0 || detailUrl.contains("video.")) {
                        continue;
                    }

                    //下载列表图片
                    String smallPicUrl = aElement.getElementsByClass("f_card_dt_img").attr("data-src");
                    if (StringUtils.isNotEmpty(smallPicUrl) && !smallPicUrl.contains("http:")) {
                        smallPicUrl = "http:" + smallPicUrl;
                    }
                    String imgPath = ImageUtil.downloadImageToDisk(smallPicUrl);

                    //获取资讯详情内容
                    news = newsDetail(detailUrl);
                    news.setTitle(title);
                    news.setSmallpicurl(imgPath);
                    news.setTypeId(3);
                } catch (Exception e) {
                    logger.error(e.getMessage());
                }

                //插入数据库
                try {
                    newsService.save(news);
                } catch (Exception e) {
                    logger.error(e.getMessage());
                }

                //等待
                Thread.sleep(Constants.millis);
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return list;
    }

//    /**
//     * 高清图列表(PC端)
//     *
//     * @return
//     */
//    public List<News> gqphotoList() {
//        List<News> list = new ArrayList<>();
//        try {
//            Document doc = JsoupUtil.getDocument("http://golf.sina.com.cn/gqphoto/");
//            Elements aElements = doc.getElementById("Conta1").getElementsByTag("a");
//            for (Element aElement : aElements) {
//                String title = aElement.attr("title");
//                String detailUrl = aElement.attr("detailUrl");
//                News news = gqphotoDetail(detailUrl);
//                news.setTitle(title);
//                news.setTypeId(6);
//            }
//        } catch (Exception e) {
//            logger.error(e.getMessage());
//        }
//        return list;
//    }
//
//    /**
//     * 高清图详情(PC端)
//     *
//     * @return
//     */
//    public News gqphotoDetail(String url) {
//        News news = new News();
//        List<NewsAlbum> newsAlbumList = new ArrayList<>();
//        try {
//            Document doc = JsoupUtil.getDocument(url);
//            Element element = doc.getElementById("eData");
//            Elements dlElements = element.getElementsByTag("dl");
//            int imgSize = dlElements.size();
//            int i = 0;
//            for (Element dlElement : dlElements) {
//                NewsAlbum newsAlbum = new NewsAlbum();
//                String title = dlElement.getElementsByTag("dt").get(0).text();
//                Elements ddElements = dlElement.getElementsByTag("dd");
//                String picUrl = ddElements.get(0).text();
//                newsAlbum.setTitle(title);
//
//                String imgPath = ImageUtil.downloadImageToDisk(picUrl);
//                newsAlbum.setPicUrl(imgPath);
//                newsAlbum.setPicUrlThumb(imgPath);
//                newsAlbum.setSort(imgSize--);
//
//                //取第一张图集的内容、时间、图片做资讯内容、时间、图片
//                if (i == 0) {
//                    if (ddElements.size() > 3) {
//                        news.setAddtime(DateConverUtil.conver(ddElements.get(3).text()));
//                    }
//                    if (ddElements.size() > 4) {
//                        news.setContent(ddElements.get(4).text());
//                    }
//                    news.setPicurl(imgPath);
//                    news.setSmallpicurl(imgPath);
//                }
//                newsAlbumList.add(newsAlbum);
//                i++;
//            }
//            news.setNewsAlbumList(newsAlbumList);
//        } catch (Exception e) {
//            logger.error(e.getMessage());
//        }
//        return news;
//    }

}
